package com.interview.a_多线程.e_Thread和Object中的重要方.f_sleep不会释放锁;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * sleep 时 不会释放 lock
 */
public class Sleep_2 implements Runnable {
    private static int count = 0;
    private Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        Sleep_2 sleep_2 = new Sleep_2();

        Thread thread1 = new Thread(sleep_2, "1");
        thread1.start();

        Thread thread2 = new Thread(sleep_2, "2");
        thread2.start();

    }

    @Override
    public void run() {
        while (count <= 2) {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "  获取到锁");
            try {
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() + "  执行完毕 \n");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                count++;
                lock.unlock();
            }

        }
    }
}
